package test;

import java.util.Arrays;

public class Resove {
    public static void main(String[] args) {
        int[] cards = {1,2,3,4,5,6,7};
        int n = 7;
        int k = 2;
        int time = 1;
        System.out.println(Arrays.toString(getRes(n,k,time,cards)));
    }

    private static int[] getRes(int n, int k, int time, int[] cards) {
        if (time > k){
            return cards;
        }
        int[] leftCards;
        int[] rightCards;
        if (n % 2 == 0){
            leftCards = Arrays.copyOfRange(cards,0,cards.length/2);
            rightCards = Arrays.copyOfRange(cards,cards.length/2,cards.length);
        }else {
            leftCards = Arrays.copyOfRange(cards,0,cards.length/2 + 1);
            rightCards = Arrays.copyOfRange(cards,cards.length/2 + 1,cards.length);
        }
        if (time % 2 != 0){
            cards = xiPai(rightCards,leftCards);
        }else {
            cards = xiPai(leftCards,rightCards);
        }
        cards = getRes(n,k,time + 1,cards);

        return cards;
    }

    private static int[] xiPai(int[] leftCards, int[] rightCards) {
        int[] res = new int[leftCards.length + rightCards.length];
        int leftIndex = leftCards.length - 1;
        int rightIndex = rightCards.length - 1;
        int index = res.length - 1;
        while (leftIndex >= 0 || rightIndex >= 0){
            if (leftIndex >= 0){
                res[index --] = leftCards[leftIndex --];
            }
            if (rightIndex >= 0){
                res[index --] = rightCards[rightIndex --];
            }
        }
        return res;
    }
}
